package com.example.yunyuapp;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SearchActivity extends AppCompatActivity {

    private ImageView imageView;
    private EditText edtSearchCity;
    private TextView tvSearchPM25;
    private TextView tvSearchCT;
    private TextView tvSearchSO2;
    private TextView tvSearchO3;
    private TextView tvSearchCO;
    private TextView tvSearchNO2;
    private TextView tvSearchAQI;
    private TextView tvSearchQuality;
    private ImageView igSearch;

    OkHttpClient okHttpClient = new OkHttpClient();
    Gson gson = new Gson();

    PM2_5 pm2_5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();

        //返回主页
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //搜索指定城市空气数据
        igSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入框城市名称
                String city = edtSearchCity.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Request.Builder reqBuild = new Request.Builder();
                        //APPCODE验证
                        reqBuild.addHeader("Authorization", "APPCODE " + MainActivity.APPCODE);
                        HttpUrl.Builder urlBuilder = HttpUrl.parse(MainActivity.URL).newBuilder();
                        //搜索指定城市
                        urlBuilder.addQueryParameter("city", city);
                        reqBuild.url(urlBuilder.build());
                        Request request = reqBuild.build();

                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        PM2_5 pm2_5 = gson.fromJson(result, PM2_5.class);

                                        //获取指定城市空气数据
                                        String pmSearch = pm2_5.getShowapi_res_body().getPm().getPm2_5();
                                        String ctSearch = pm2_5.getShowapi_res_body().getPm().getCt();
                                        String so2Search = pm2_5.getShowapi_res_body().getPm().getSo2();
                                        String o3Search = pm2_5.getShowapi_res_body().getPm().getO3();
                                        String coSearch = pm2_5.getShowapi_res_body().getPm().getCo();
                                        String no2Search = pm2_5.getShowapi_res_body().getPm().getNo2();
                                        String aqiSearch = pm2_5.getShowapi_res_body().getPm().getAqi();
                                        String qualitySearch = pm2_5.getShowapi_res_body().getPm().getQuality();

                                        //添加指定城市空气数据
                                        tvSearchPM25.setText("PM2.5：" + pmSearch);
                                        tvSearchCT.setText("时间：" + ctSearch);
                                        tvSearchSO2.setText("SO2：" + so2Search);
                                        tvSearchO3.setText("O3：" + o3Search);
                                        tvSearchCO.setText("CO：" + coSearch);
                                        tvSearchNO2.setText("NO2：" + no2Search);
                                        tvSearchAQI.setText("空气指数：" + aqiSearch);
                                        tvSearchQuality.setText("空气质量：" + qualitySearch);
                                    }
                                });
                            }

                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {

                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        edtSearchCity = (EditText) findViewById(R.id.edtSearchCity);
        tvSearchPM25 = (TextView) findViewById(R.id.tvSearchPM2_5);
        tvSearchCT = (TextView) findViewById(R.id.tvSearchCT);
        tvSearchSO2 = (TextView) findViewById(R.id.tvSearchSO2);
        tvSearchO3 = (TextView) findViewById(R.id.tvSearchO3);
        tvSearchCO = (TextView) findViewById(R.id.tvSearchCO);
        tvSearchNO2 = (TextView) findViewById(R.id.tvSearchNO2);
        tvSearchAQI = (TextView) findViewById(R.id.tvSearchAQI);
        tvSearchQuality = (TextView) findViewById(R.id.tvSearchQuality);
        igSearch = (ImageView) findViewById(R.id.igSearch);
    }
}